# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=ansicon
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=1.70.r65.3acc7a9
pkgrel=2
pkgdesc="Process ANSI escape sequences for Windows console programs (mingw-w64)"
arch=('any')
url="http://adoxa.altervista.org/ansicon/"
license=("zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
source=("git+https://github.com/adoxa/ansicon.git"
        "0001-Do-not-invoke-cmd-in-makefile.patch"
        "ansicon.bat")
sha256sums=('SKIP'
            '89d247f6fc888e66bd1b1982bf565b4b08bbff476063a380e6553a3d5ff7fad2'
            '715175d22838e71a15c24a90a6ca21ba5131aa98a7c437e6c62a7c83c4227486')

pkgver() {
  cd ${srcdir}/${_realname}
  printf "%s.r%s.%s" "$(grep PVERSA version.h | sed -r 's/.*"(.+)".*/\1/')" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -p1 -i "${srcdir}/0001-Do-not-invoke-cmd-in-makefile.patch"
}

build() {
  local _bit
  case ${CARCH} in
  x86_64) _bit=64 ;;
  i686) _bit=32 ;;
  esac

  cd "${srcdir}/${_realname}"
  ARCH=${_bit} make -f makefile.gcc
}

package() {
  local _arch _bit
  case ${CARCH} in
  x86_64) _arch=64; _bit=64 ;;
  i686) _arch=86; _bit=32 ;;
  esac

  cd "${srcdir}/${_realname}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  cp "${srcdir}/ansicon.bat" "${pkgdir}${MINGW_PREFIX}/bin"
  cp "x${_arch}/ansicon.exe" "${pkgdir}${MINGW_PREFIX}/bin/ansicon_x${_arch}.exe"
  cp "x${_arch}/ansi${_bit}.dll" "${pkgdir}${MINGW_PREFIX}/bin/ansi${_bit}.dll"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp readme.* sequences.txt G1.* "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  cp LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
